<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_mdstruktur extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_mdstruktur_model', 'Mdstruktur');
		$this->load->model('kst_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Mdstruktur";
		$data['comboinduk'] = $this->Mdstruktur->getApp();
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		$data['arr_type_1']	= $this->Combo->type_div();
		$data['arr_type_2']	= $this->Combo->type_dev();
		$data['arr_type_3']	= $this->Combo->type_cls();
		
		viewByTemplate('km_mdstruktur/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Mdstruktur->GetJSON();
	}

	private function validation($val)
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		$sql = $this->db->query("SELECT COUNT(*) ".'"total"'." FROM km_mdstruktur WHERE mdstruktur_path = '".$val."'");
		$row = $sql->row_array();


		//var_dump($this->get_data("SELECT 1 FROM km_mdstruktur WHERE mdstruktur_path = post(mdstruktur_path)" > 0);
		//mdstruktur_id, mdstruktur_pid, mdstruktur_kode, mdstruktur_nama, mdstruktur_type, mdstruktur_margin, mdstruktur_path
		if (post("mdstruktur_kode") == '') {
			$error->Add("mdstruktur_kode", "Kode Mdstruktur", $tpl_empty);
		} else if (post("mdstruktur_nama") == '') {
			$error->Add("mdstruktur_nama", "Nama Mdstruktur", $tpl_empty);
		} else if (post("mdstruktur_type") == '') {
			$error->Add("mdstruktur_type", "Type Mdstruktur", $tpl_empty);
//		} else if (post("mdstruktur_path") == '') {
//			$error->Add("mdstruktur_path", "Path Mdstruktur", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_mdstruktur WHERE mdstruktur_path = '".$val."'")) > 0) {
			$error->Add("mdstruktur_kode", "Kode Mdstruktur", $tpl_duplicate);
		}		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		if($this->input->post('mdstruktur_type') == 'DEP' || $this->input->post('mdstruktur_type') == 'CLS'){
					$mdstruktur_path = $this->input->post('mdstruktur_path').".".$this->input->post('mdstruktur_kode');
				}else{
					$mdstruktur_path = $this->input->post('mdstruktur_kode'); 
				}

		$error = $this->validation($mdstruktur_path);
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{

				$query = $this->db->query("SELECT mdstruktur_id FROM (SELECT * FROM km_mdstruktur ORDER BY mdstruktur_id DESC) WHERE rownum <= 1");
				$getquery = $query->row_array();
				$rowcount = $getquery['mdstruktur_id'];
				$autonum = $rowcount + 1;
				$data = array(
								'mdstruktur_id' => $autonum,
								'mdstruktur_pid' => $this->input->post('mdstruktur_pid'),
								'mdstruktur_kode' => $this->input->post('mdstruktur_kode'),
								'mdstruktur_nama' => $this->input->post('mdstruktur_nama'),
								'mdstruktur_type' => $this->input->post('mdstruktur_type'),
								'mdstruktur_path' => $mdstruktur_path,
								'mdstruktur_margin' => $this->input->post('mdstruktur_margin'),
							  );

				// Fill Post Data
				//$data	= post_by_key(array('mdstruktur_pid', 'mdstruktur_kode', 'mdstruktur_nama', 'mdstruktur_type', 'mdstruktur_margin', 'mdstruktur_path'));
		//		$data	= post_by_key(array('mdstruktur_pid','mdstruktur_kode', 'mdstruktur_nama', 'mdstruktur_type', 'mdstruktur_margin'));
						$data['mdstruktur_kode']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['mdstruktur_kode'])));		
	//		$data['mdstruktur_nama']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['mdstruktur_nama'])));
	//		$data['mdstruktur_type']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['mdstruktur_type'])));
				
				if (post("mode") == "create")
				{
					$this->Mdstruktur->Add($data);
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					$this->Mdstruktur->Update($data, array('mdstruktur_kode'	=> $data['mdstruktur_kode']));
					$ls_response = "Data Tersimpan";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$mdstruktur_kode	= get('mdstruktur_kode');
		
		if ($mdstruktur_kode != "")
		{
			// Delete Single
			$arr_rule	= array('mdstruktur_kode' => $mdstruktur_kode);
			$ls_result = $this->Mdstruktur->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('mdstruktur_kode' => $row['mdstruktur_kode']);
				$ls_result .= $this->Mdstruktur->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

	function getType(){
		echo $this->Mdstruktur->getType($this->input->post('option'));
	}
}
?>